package com.newvpn.fastbest.zenith.notify;

import static com.newvpn.fastbest.zenith.presenter.VPNReulst.STATE_DISCONNECT;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.widget.RemoteViews;

import androidx.core.app.NotificationCompat;

import com.newvpn.fastbest.zenith.R;
import com.newvpn.fastbest.zenith.activity.MainActivity;
import com.newvpn.fastbest.zenith.presenter.VPNPresenter;

public class DisConnectNotifyHelper {

    private static final int NOTIFICATION_ID = 101;
    private static final String CHANNEL_ID = "DisConnectNotifyHelper";
    private static final String CHANNEL_NAME = "DisConnectNotifyHelper";

    public static void send(Context context) {
        if (VPNPresenter.INSTANCE.getVPNStatus() != STATE_DISCONNECT) {
            return;
        }
        // 创建意图
        Intent singleIntent = new Intent(context, MainActivity.class);
        singleIntent.putExtra("source", 1);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                context,
                NOTIFICATION_ID,
                singleIntent,
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_IMMUTABLE : PendingIntent.FLAG_UPDATE_CURRENT
        );

        // 创建自定义通知布局
        RemoteViews customLayout = new RemoteViews(context.getPackageName(), R.layout.v_notify_disconnet_remote);
        // 创建通知渠道
        customLayout.setOnClickPendingIntent(R.id.root_view, pendingIntent);
        createNotificationChannel(context);
        // 创建通知
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setSmallIcon(com.github.shadowsocks.core.R.mipmap.ic)
                .setCustomHeadsUpContentView(customLayout)//横幅样式显示的布局
                .setContent(customLayout)
                .setContentIntent(pendingIntent)
                .setCustomContentView(customLayout)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setSound(null)
                .setVibrate(null)
                .setOngoing(true);

        Notification notification = builder.build();

        // 显示通知
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager != null) {
            notificationManager.notify(NOTIFICATION_ID, notification);
        }
    }

    public static void cancel(Context context) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager != null) {
            notificationManager.cancel(NOTIFICATION_ID);
        }
    }


    public static void createNotificationChannel(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    CHANNEL_ID,
                    CHANNEL_NAME,
                    NotificationManager.IMPORTANCE_DEFAULT);
            //channel.setDescription(CHANNEL_DESCRIPTION);

            NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }
        }
    }
}